$(function () {
    var dataList = JSON.parse(Cookies.get('carts') || '[]')
    var strHtml = ``
    dataList.forEach(function (item) {
        strHtml += `
            <tr style="color: #666;">
                <td>
                    <span style="float: left;text-align: left; line-height: 1.4em; color: #666;"class="cart-span1">
                        <a href="" style="border: 0px none; color:#666">
                            <img src=${item.img} alt=""style="cursor: pointer; width: 50px;height: 50px;">
                        </a>
                    </span>
                </td>
                <td>
                    <span class="cart-span2">
                        <a href="//www.s.cn/originals-EG4958.html" target="_blank" style="color: #666;">
                            ${item.txt}
                            <i>(&nbsp;尺码:${item.size},颜色:${item.color}&nbsp;)</i>
                        </a>
                    </span>
                </td>
                <td>0</td>
                <td class="cart-price">${item.price}</td>
                <td>
                    <div id="onlyId" style="display:none">${item.id}</div>
                    <div id="onlySize" style="display:none">${item.size}</div>
                    <span class="numBox">
                        <span class="incerse"></span>
                        <input type="text" name="" class="num" value="${item.num}" disabled>
                        <span class="add"></span>
                    </span>
                </td>
                <td class="f_yh"> - </td>
                <td class="allPrice"> ${item.num * item.price} </td>
                <td>
                    <a href="javascript:void(0)" style="color: #666;" class="delete">删除</a>
                </td>
            </tr>
        
        `
    })
    $('.cart_buyBox').html(strHtml)
    sumMoney()
    function sumMoney() {
        var money = 0;
        var num=0
        for (let i = 0; i < $('.cart_buyBox').children().length; i++) {
            money += Number($('.cart_buyBox').children().eq(i).find('.allPrice').text())
            num+=Number($('.cart_buyBox').children().eq(i).find('input').val())
        }
        $('#allMoney').html(money)
        console.log(num);
        $('#allCartNum').html(num)
    }
    $('.cart_buyBox').find('.incerse').on('click', function () {
        var num = $(this).next().val()
        if (num <= 1) {
            return
        }
        num = num - 1
        $(this).next().val(num)

        var $litMoney = $(this).parents('tr').find('.allPrice')
        var $allMoney = $(this).parents('tr').find('.cart-price').text() * $(this).next().val()
        $litMoney.html($allMoney)
        var $id = $(this).parents('td').find('#onlyId').text()
        var $cartSize = $(this).parents('td').find('#onlySize').text()
        for (var i = 0; i < dataList.length; i++) {
            if (dataList[i].id == $id && dataList[i].size == $cartSize) {
                dataList[i].num = num
                break
            }
        }
        Cookies.set('carts', JSON.stringify(dataList), {
            expires: 10
        });
        sumMoney()


    })
    $('.cart_buyBox').find('.add').on('click', function () {
        var num = $(this).prev().val()
        num = parseInt(num) + 1
        $(this).prev().val(num)
        var $litMoney = $(this).parents('tr').find('.allPrice')
        console.log($(this).parents('tr').find('.cart-price').text());
        console.log($litMoney);
        console.log($(this).prev().val());
        var $allMoney = $(this).parents('tr').find('.cart-price').text() * $(this).prev().val()
        // console.log($allMoney);
        $litMoney.html($allMoney)
        var $id = $(this).parents('td').find('#onlyId').text()
        var $cartSize = $(this).parents('td').find('#onlySize').text()
        for (var i = 0; i < dataList.length; i++) {
            if (dataList[i].id == $id && dataList[i].size == $cartSize) {
                dataList[i].num = num
                break
            }
        }
        Cookies.set('carts', JSON.stringify(dataList), {
            expires: 10
        });
        sumMoney()
    })
    $('.delete').on('click', function () {
        var self=this
        console.log(this);
        layer.confirm('您确定要删除吗?', {
            btn: ['是', '否']
        }, function (index) {
            var $id = $(self).parents('tr').find('#onlyId').text()
            var $cartSize = $(self).parents('tr').find('#onlySize').text()
            console.log( $id,$cartSize);
            for (var i = 0; i < dataList.length; i++) {
                if (dataList[i].id == $id && dataList[i].size == $cartSize) {
                    dataList.splice(i, 1)
                    break
                }
            }
            Cookies.set("carts", JSON.stringify(dataList), {
                expires: 10
            })
            $(self).parents('tr').remove();
            sumMoney()

            layer.close(index)

        }, function () {
            return
        });
    })

    $('.buyBuy').on('click',function(){
        layer.msg('购买成功');
        
    })

})